package com.fengye.school.service;

import com.fengye.school.model.school.Grade;
import com.fengye.school.model.school.Teacher;
import com.fengye.school.response.ResponsePageResult;
import com.fengye.school.respository.GradeRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class GradeService {

    @Resource
    private GradeRepository gradeRepository;

    public Grade save(Grade grade){
        Grade save = gradeRepository.save(grade);
        log.info("保存成功, grade={}", save);
        return save;
    }

    public List<Grade> list() {
        return gradeRepository.findAll();
    }

    public ResponsePageResult<Grade> listByPage(int page, int size) {
        Pageable pageRequest = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");
        Page<Grade> all = gradeRepository.findAll(pageRequest);
        return ResponsePageResult.success("获取所有年级信息成功", all.getTotalElements(), all.getContent());
    }

    public void deleteAll(Long[] ids) {
        Arrays.stream(ids).forEach(id -> gradeRepository.deleteById(id));
    }

    public void deleteById(Long id) {
        gradeRepository.deleteById(id);
    }
}
